package com.mnt.gui.base.controller;

import javafx.collections.MapChangeListener;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import com.mnt.gui.base.data.TableData;

/**
 * javafx base controller
 * @author mnt.cico
 *
 */
public abstract class BaseController extends VBox {

	protected static Stage stage; 
	
	private boolean isStartTransferListener = false;
	
	protected BaseController() {
		
	}
	
	/**
	 * if you want load resource, you need override this function
	 */
	public void init() {
		
	};
	
	/**
	 * set stage (stage from BaseLauncher)
	 * @param stage
	 */
	public static void setStage(Stage stage) {
		BaseController.stage = stage;
	}
	
	/**
	 * 
	 * @return main view stage
	 */
	public Stage getStage() {
		return stage;
	}
	
	void configureComponentTransfer()
	{
		if (!isStartTransferListener)
		{
			isStartTransferListener = true;
			getProperties().addListener(new MapChangeListener<Object, Object>()
			{
				@Override
				public void onChanged(javafx.collections.MapChangeListener.Change<? extends Object, ? extends Object> objectMap)
				{
					if (null != objectMap.getMap().get(objectMap.getKey()))
					{
						//if the key is common object
						if (objectMap.getKey().equals(ControllerIntent.DEFAULT_TRANSFER_DATA_KEY))
						{
							receiveData(objectMap.getMap().remove(objectMap.getKey()));
						} else
						{
							receiveDataAndKey(objectMap.getKey(), objectMap.getMap().remove(objectMap.getKey()));
						}
					}
				}

			});

		}
	}

	/**
	 * receive data with DEFAULT_TRANSFER_DATA_KEY 
	 * 2014-3-29 mnt.cico
	 */
	protected void receiveData(Object value){
		
	}

	/**
	 * receive data with key and value 
	 * 2014-3-29 mnt.cico
	 */
	protected void receiveDataAndKey(Object key, Object value) {
		
	}
	
	/**
	 * External execution 
	 * 2014-6-30 mnt.cico
	 * @param object
	 */
	public void outExecute(Object object) {
		
	}
	
	/**
	 * overwrite show the view execute
	 * 2014年7月5日 mnt.cico
	 */
	public void showAndExecute() {
		
	}
	
	public boolean disableSame(Object obj) {
		return false;
	}
	
	/**
	 * 
	 * <p>
	 * get table date
	 * </p>
	 * @create mnt.cico
	 * @return
	 */
	public TableData[] getTableData() {
		final TableData[] result = new  TableData[0]; 
		return result;
	}
	
	/**
	 * check can print 
	 * @create mnt.cico
	 * @return
	 */
	public boolean checkPrint() {
		return false;
	}
	
	/**
	 * 
	 * <p>
	 * check can export excel 
	 * </p>
	 * @create mnt.cico
	 * @return
	 */
	public boolean checkExcel() {
		return false;
	}
	
}
